package com.flame.shapeville.component.task4;

import javafx.concurrent.Worker;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.scene.web.WebView;

/**
 * Represents a circle defined by its radius with center point and radius line.
 * Used in Task 4 for visualization and formula explanation.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-05-14
 */
public class JfCircleRC extends JfShape {

    /**
     * Radius of the circle.
     */
    private int radius;

    /**
     * X-coordinate center of the circle.
     */
    private int x;

    /**
     * Y-coordinate center of the circle.
     */
    private int y;

    /**
     * Constructs a new circle using given radius.
     *
     * @param radius the radius of the circle
     */
    public JfCircleRC(int radius) {
        super();
        this.radius = radius;
        this.x = (int) shape.getPrefWidth() / 2;
        this.y = (int) shape.getPrefHeight() / 2;
        initCircle();
        initDetailed();
        initTranslate();
    }

    /**
     * Draws the circle with center point and radius line.
     */
    private void initCircle() {
        int r = radius < 7 ? size / 3 : radius < 14 ? size / 12 * 5 : size / 2;
        Circle circle = new Circle(x, y, r);
        circle.setFill(null);
        circle.setStroke(Color.LIGHTBLUE);
        circle.setStrokeWidth(4);

        Circle centPoint = new Circle(x, y, 4);
        centPoint.setFill(Color.BLACK);

        Line radiusLine = new Line(x, y, x + r, y);
        radiusLine.setStrokeWidth(2);
        radiusLine.setStroke(Color.ORANGE);

        Text label = new Text(x + 15, y - 10, "r = " + radius);
        label.setFill(Color.ORANGE);
        label.setStyle("-fx-font-size: 16px");

        shape.getChildren().addAll(circle, radiusLine, centPoint, label);
    }

    /**
     * Initializes the detailed view of the circle.
     * Includes formula explanation using an embedded web view.
     */
    @Override
    void initDetailed() {
        int r = radius < 7 ? size / 3 : radius < 14 ? size / 12 * 5 : size / 2;
        Circle circle = new Circle(x / 2, y, r);
        circle.setFill(null);
        circle.setStroke(Color.LIGHTBLUE);
        circle.setStrokeWidth(4);

        Circle centPoint = new Circle(x / 2, y, 4);
        centPoint.setFill(Color.BLACK);

        Line radiusLine = new Line(x / 2, y, x / 2 + r, y);
        radiusLine.setStrokeWidth(2);
        radiusLine.setStroke(Color.ORANGE);

        Text label = new Text(x / 2 + 15, y - 10, "r = " + radius);
        label.setFill(Color.ORANGE);
        label.setStyle("-fx-font-size: 16px");

        components.add(circle);
        components.add(centPoint);
        components.add(radiusLine);
        components.add(label);

        Line dashedLine = new Line();
        dashedLine.setStartX(0);
        dashedLine.setStartY(20);
        dashedLine.setEndX(0);
        dashedLine.setEndY(220);
        dashedLine.setStroke(Color.GRAY);
        dashedLine.setStrokeWidth(2);
        dashedLine.getStrokeDashArray().addAll(10d, 4d);
        dashedLine.setTranslateX(x - 1);

        WebView webView = new WebView();
        webView.setPrefSize(220, 240);
        webView.setLayoutX(x + 1);
        webView.setStyle("-fx-background-color: transparent;");
        String url = getClass().getResource("/static/formula/task4/circle_rc.html").toExternalForm();
        webView.getEngine().load(url);
        webView.getEngine().getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                webView.getEngine().executeScript("updateFormula(" + radius + ")");
            }
        });

        detailedShape.getChildren().addAll(dashedLine, webView, circle, radiusLine, centPoint, label);
    }
}
